package Operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration{
    public void work(BookList bookList) {
        System.out.println("删除图...");
        System.out.println("请选择你要删除的图书:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        //遍历数组 是否存在需要的书
        int index = -1;
        int currentsize = bookList.getUsedsize();
        int i = 0;
        for(;i < currentsize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                index = i;
                break;
            }
        }
        if(i >= currentsize) {
            System.out.println("没有你要删除的书....");
            return;
        }
        for (int j = index; j < currentsize-1; j++) {
            //bookList[j] = bookList[j+1];
            Book book = bookList.getBook(j+1);
            bookList.setBooks(j,book);
        }
        bookList.setUsedsize(currentsize - 1);
        System.out.println("删除书籍成功....");
    }
}
